package com.gsi.webstore.platform.storefront.pipelet.orderprocess;

import com.gsi.webstore.platform.adapter.storelocator.internal.client.data.Country;
import com.gsi.webstore.platform.adapter.storelocator.internal.client.data.Storefront;
import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import java.util.List;

/**
 * Map the store front id's to the appropriate country object.
 */
public class CreateStorefrontCountryMap extends Pipelet
{

    /**
     * Constant used to access the pipeline dictionary with key 'StoreFrontMap'
     * 
     * Map of storefront id's to storefront objects
     */
    public static final String DN_STORE_FRONT_MAP = "StoreFrontMap";
    /**
     * Constant used to access the pipeline dictionary with key 'CountryList'
     * 
     * List of Country objects
     */
    public static final String DN_COUNTRY_LIST = "CountryList";
    /**
     * Constant used to access the pipeline dictionary with key 'StoreFrontCountryMap'
     * 
     * Map of storefront id's to country objects
     */
    public static final String DN_STORE_FRONT_COUNTRY_MAP = "StoreFrontCountryMap";
    /**
     * Constant used to access the pipeline dictionary with key 'CountryMap'
     * 
     * Map of country codes the country names from store locator
     */
    public static final String DN_COUNTRY_MAP = "CountryMap";

    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict)
    throws PipeletExecutionException {        
        // lookup 'CountryList' in pipeline dictionary
        @SuppressWarnings("unchecked")
        List<Country> countryList = (List<Country>)dict.get(DN_COUNTRY_LIST);
        if (null == countryList)
        {
            throw new PipeletExecutionException("Mandatory input parameter 'CountryList' not available in pipeline dictionary.");
        
        }
        
        // lookup 'StoreFrontMap' in pipeline dictionary
        @SuppressWarnings("unchecked")
        Map<Long, Storefront> storeFrontMap = (Map<Long, Storefront>)dict.get(DN_STORE_FRONT_MAP);
        if (null == storeFrontMap)
        {
            throw new PipeletExecutionException(
                            "Mandatory input parameter 'StoreFrontMap' not available in pipeline dictionary.");
        
        }

        Map<String, String> countryMap = createCountryMap(countryList);
        Map<Long, String> storeFrontCountryMap = new HashMap<Long, String>();
        Iterator<Map.Entry<Long, Storefront>> storeFrontIterator = storeFrontMap.entrySet().iterator();
        while (storeFrontIterator.hasNext()) {
            Map.Entry<Long, Storefront> entry = storeFrontIterator.next();
            Storefront storefront = entry.getValue();
            storeFrontCountryMap.put(storefront.getId(), countryMap.get(storefront.getCountryCode()));
        }
        
        dict.put(DN_STORE_FRONT_COUNTRY_MAP, storeFrontCountryMap);
        dict.put(DN_COUNTRY_MAP, countryMap);
        
        return PIPELET_NEXT;
    }
    
    private Map<String, String> createCountryMap(List<Country> countryList) {
        Map<String, String> countryMap = new HashMap<String, String>();
        for (Country country : countryList) {
            countryMap.put(country.getCode(), country.getName());
        }
        
        return countryMap;
    }
}